//
//  BonusesView.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/22.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "BonusesView.h"
#import "HengYouCai-Swift.h"
@interface BonusesView ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableViewCell *_currentCell;
    NSIndexPath *_currentIndexPath;
    BOOL _isSclecd;
}
@property (nonatomic, retain)UITableView *myTableView;
@property (nonatomic, retain)NSArray *totoalArr;
@property (nonatomic, retain)UIButton *certainBtn;
@property (nonatomic, retain)UIView *bgView;
@property (nonatomic, retain)UILabel *titleLab;
@property (nonatomic, copy)NSString *title;
@property (nonatomic, copy)NSString *investBeginMoney;
@property (nonatomic, copy)NSString *limitValue;
@property (nonatomic, copy)NSString *limitType;
@property (nonatomic, copy)NSString *cellType;// 用来区分红包 加息券
@end


@implementation BonusesView

+ (instancetype)initWithFrame:(CGRect)frame currentType:(NSString*)currentType investBeginMoney:(NSString *)investBegin serviceConditionsLimitType:(NSString *)limitType serviceConditionsLimitValue:(NSString *)limitValue currentIndex:(NSInteger)index title:(NSString *)title dataArray:(NSArray *)datas bonuseBlock:(BonusesBlock)bounseBlock
{
    
    return [[BonusesView alloc] initWithFrame:frame currentTypes:currentType investBeginMoney:investBegin serviceConditionsLimitType:limitType serviceConditionsLimitValue:limitValue currentIndex:index title:title dataArray:datas bonuseBlock:bounseBlock];
}

- (instancetype)initWithFrame:(CGRect)frame currentTypes:(NSString*)currentTypes investBeginMoney:(NSString *)investBegin serviceConditionsLimitType:(NSString *)limitType serviceConditionsLimitValue:(NSString *)limitValue currentIndex:(NSInteger)index title:(NSString *)title dataArray:(NSArray *)datas bonuseBlock:(BonusesBlock)bounseBlock
{
    if (self = [super initWithFrame:frame]) {
        //
        if (index!=10000) {
            _currentIndexPath = [NSIndexPath indexPathForRow:index inSection:0];
            _isSclecd = YES;
        }
        self.title = title;
        self.totoalArr = datas;
        _bonuseBlock = bounseBlock;
        self.investBeginMoney = investBegin?:@"";
        self.limitValue = limitValue?:@"";
        self.limitType = limitType?:@"";
        self.cellType = currentTypes;
        self.backgroundColor = [UIColor colorWithWhite:.5 alpha:.2];
        [self addSubview:self.bgView];
        
        
        UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, f_Device_w, f_Device_h-350)];
        topView.backgroundColor = [UIColor clearColor];
        [self addSubview:topView];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selfHide)];
        [topView addGestureRecognizer:tap];
        
        UIWindow *window = [UIApplication sharedApplication].delegate.window;
        [window addSubview:self];
        
    }
    return self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _totoalArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZYReturnRedbagVCCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([_cellType isEqualToString:@"-100"]) {
        [cell wl_touZiConfigurationCell:_totoalArr :tableView :indexPath];
    }else{
        [cell wl_touZiCouponConfigurationCellWithModelTemp:_totoalArr listView:tableView indexPath:indexPath];
        
    }
    /// 选中状态的红包或是加息券背景图改成选中状态
    if (indexPath==_currentIndexPath) {
        
        if ([_cellType isEqualToString:@"-100"]) {
            cell.cv.image = [UIImage imageNamed:@"cash_bonus"];
        }else{
            cell.cv.image = [UIImage imageNamed:@"interest_bonus"];
        }
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_currentIndexPath==indexPath) {
        _isSclecd = !_isSclecd;

    }else{
        _isSclecd = YES;
    }
    ZYReturnRedbagVCCell *currentCell = [tableView cellForRowAtIndexPath:_currentIndexPath];
    UIImageView *imgView = currentCell.cv;
    if (!_isSclecd) {
        // 没有选择红包加息券 把之前选中的红包加息券改成不选中的背景图
        if ([_cellType isEqualToString:@"-100"]) {
            imgView.image = [UIImage imageNamed:@"cash_unbonus"];
        }else{
            imgView.image = [UIImage imageNamed:@"interest_unbonus"];
        }
        
    }else{
        // 如果选则了红包加息券 就把之前的红包加息券改成未选中状态
        if ([_cellType isEqualToString:@"-100"]) {
            imgView.image = [UIImage imageNamed:@"cash_unbonus"];
        }else{
            imgView.image = [UIImage imageNamed:@"interest_unbonus"];
        }
        // 再把当前选中的红包加息券改成选中状态
        ZYReturnRedbagVCCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        UIImageView *curimg = cell.cv;
        if ([_cellType isEqualToString:@"-100"]) {
            // 红包
            curimg.image = [UIImage imageNamed:@"cash_bonus"];
        }else{
            // 加息券
            curimg.image = [UIImage imageNamed:@"interest_bonus"];
        }
        //    _currentCell = cell;
        _currentIndexPath = indexPath;
    }
    
}
- (void)finish
{
    [UIView animateWithDuration:.65 animations:^{
    
        _bgView.frame = CGRectMake(0, f_Device_h, f_Device_w, 0);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];

    if (_isSclecd) {
//        QuanModel *model = _totoalArr[_currentIndexPath.row];
        NSString *indexRow = [NSString stringWithFormat:@"%ld",_currentIndexPath.row];;
        _bonuseBlock(indexRow);
    }else{
        _bonuseBlock(@"10000");

    }
    
}
- (NSArray *)totoalArr
{
    if (!_totoalArr) {
        _totoalArr = [NSArray array];
    }
    return _totoalArr;
}
- (UIButton *)certainBtn
{
    if (!_certainBtn) {
        _certainBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _certainBtn.frame = CGRectMake(f_Device_w-50, 15, 35, 20);
        _certainBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [_certainBtn setTitle:@"完成" forState:UIControlStateNormal];
        [_certainBtn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:0];
        [_certainBtn addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
    }
    return _certainBtn;
}
- (UITableView *)myTableView
{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 45, f_Device_w, 350-45) style:UITableViewStylePlain];
        _myTableView.showsHorizontalScrollIndicator = NO;
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.backgroundColor = Normal_BackColor;
        UIView *header  = [[UIView alloc] initWithFrame:CGRectMake(0, 0, f_Device_w, 20)];
        header.backgroundColor = Normal_BackColor;
        _myTableView.tableHeaderView = header;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_myTableView registerClass:[ZYReturnRedbagVCCell class] forCellReuseIdentifier:@"CELLID"];
    }
    return _myTableView;
}
- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, f_Device_h-350, f_Device_w, 350)];
        _bgView.backgroundColor = [UIColor whiteColor];
//        _bgView.userInteractionEnabled = NO;

        
        [_bgView addSubview:self.titleLab];
                                   
        [_bgView addSubview:self.certainBtn];
        [_bgView addSubview:self.myTableView];
        
    }
    return _bgView;
}
- (UILabel *)titleLab
{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] init];
        _titleLab.center = CGPointMake(f_Device_w/2.0, 22.5);
        _titleLab.bounds = CGRectMake(0, 0, 100, 30);
        _titleLab.font = [UIFont systemFontOfSize:16];
        _titleLab.textColor = Normal_333Color;
        _titleLab.textAlignment = NSTextAlignmentCenter;
        _titleLab.text = _title;
        
    }
    return _titleLab;
}
- (void)selfHide
{
    [UIView animateWithDuration:.65 animations:^{
        
        _bgView.frame = CGRectMake(0, f_Device_h, f_Device_w, 0);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
